package com.yonyou.crm.tpub.attachment.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.common.model.util.ModelUtil;
import com.yonyou.crm.sys.user.service.UserBizServiceImpl;
import com.yonyou.crm.tpub.attachment.entity.AttachmentDO;
import com.yonyou.crm.tpub.attachment.entity.AttachmentVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class AttachmentBizServiceImpl extends BaseBizServiceImpl<AttachmentDO>{
	
	@Autowired
	private UserBizServiceImpl userService;
	
	/**
	 * 附件列表
	 * @param param
	 * @return
	 */
	public List<AttachmentVO> getList(Map<String, Object> param){
		String tenantIdStr = InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		param.put("tenantId", tenantId);
		List<AttachmentDO> doList = dao.selectVoList(AttachmentDO.class, "getAttachmentList", param);
		List<AttachmentVO> voList = ModelUtil.copyModelList(doList, AttachmentVO.class);
		trans(voList);
		return voList;
	}
	
	private List<AttachmentVO> trans(List<AttachmentVO> voList) {
		
		if (voList != null && voList.size() > 0) {
			List<FieldRelation<AttachmentVO>> relations = new ArrayList<FieldRelation<AttachmentVO>>();
			relations.add(new RefFieldRelation<AttachmentVO>(AttachmentVO.class, "uploadUserId", new String[]{"uploadUserName"}, userService, "selectFieldsByIds"));
			new FieldRelationHandler<AttachmentVO>().handler(voList, relations);
		}

		return voList;
	}
	
	/**
	 * 附件详情
	 * @return
	 */
	public AttachmentVO getAttachment(Long id){
		AttachmentDO aDO = super.getVOById(AttachmentDO.class, id);
		AttachmentVO vo = ModelUtil.copyModel(aDO, AttachmentVO.class);
		trans(Arrays.asList(vo));
		return vo;
	}
	
	/**
	 * 附件新增
	 * @return
	 */
	public AttachmentVO save(AttachmentVO vo){

		vo.setUploadTime(new Date());
		vo.setUploadUserId(new Long(InvocationInfoProxy.getParameter("id")));
		vo.setTenantId(Long.parseLong(InvocationInfoProxy.getTenantid()));
		vo.setServerId(1L);
		
		AttachmentDO aDO = super.saveVO(ModelUtil.copyModel(vo, AttachmentDO.class));
		vo = ModelUtil.copyModel(aDO, AttachmentVO.class);
		trans(Arrays.asList(vo));
		return vo;
	}
	
	/**
	 * 附件删除
	 */
	public int batchDelete(String[] idStrs){
		return dao.batchDelete(AttachmentDO.class, idStrs);
	}
	
	/**
	 * 字段附件查看
	 * @param request
	 * @param response
	 * @return
	 */
	public List<AttachmentVO> queryAttachment(List<Long> idList){
		Long[] picIdArr = (Long[])idList.toArray(new Long[idList.size()]);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", picIdArr);
		List<AttachmentVO> attachmentList = getList(param);
		return attachmentList;
	}
	
	/**
	 * 名称是否存在
	 * @param objType
	 * @param objId
	 * @param name
	 * @return
	 */
	public boolean isExistName(Integer objType, Long objId, String name) {
		
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("objType", objType);
		paraMap.put("objId", objId);
		paraMap.put("name", name);
		paraMap.put("tenantId", InvocationInfoProxy.getTenantid());
		
		return getCountByCondition(paraMap) > 0;
	}
	
	/**
	 * 根据对象id获得附件数量
	 * @param objType
	 * @param objId
	 * @return
	 */
	public int getCountByObjId(Integer objType, Long objId) {
		
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("objType", objType);
		paraMap.put("objId", objId);
		paraMap.put("tenantId", InvocationInfoProxy.getTenantid());
		return getCountByCondition(paraMap);
	}
	
	/**
	 * 根据条件查询附件数量
	 * @param paraMap
	 * @return
	 */
	public int getCountByCondition(Map<String, Object> paraMap) {
		return dao.countAll(AttachmentDO.class, "getCountByCondition", paraMap);
	}

}
